<?php
/**
 * 媒资管理
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */

namespace app\api\controller\tagmanage;

use app\api\controller\common\ApiBase;
use app\api\model\BaseCategory;
use think\Db;

class Main extends ApiBase
{

    protected $admin;
    protected $model = null;

    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = new BaseCategory();
    }


    function getList()
    {


        $data = $this->request->param();
        $listRows = $data['listRows'] ?? 10;
        $page = $data['page'] ?? 1;
        $map = [
            'is_delete' => 0,
            'parent_id' => 0
        ]; 
        
        if (isset($data['title']) && $data['title']) { 
            $map['title'] = ['like', '%'.$data['title'].'%'];
        }
        // 先以启用，警示，停用排序，再以创建时间排序，创建时间近的排前
        $sort = 'status desc,iswarn desc,create_time desc';
        $list = $this->model
            ->where($map)
            ->order($sort)
            ->paginate($listRows, false, [
                'page' => $page
            ])->toArray();

        $map = [
            'is_delete' => 0,
            'parent_id' => [
                'in',
                array_column($list['data'], 'id')
            ]
        ];

        $second_list = $this->model
            ->where($map)
            ->order('sort desc')
            ->select();

         foreach ($list['data'] as &$v) {
            $v['children'] = [];
            if ($second_list) {
                foreach ($second_list as &$vv) {
                    if ($vv['parent_id'] == $v['id']) {
                        $v['children'][] = $vv;
                    }
                }
            }

        }

        $this->success('获取成功', '', $list);
    }

    public function getList_discard()
    {
        $parent = db('jq_base_cate')->where([
            'is_delete' => 0,
            'parent_id' => 0
        ])->order('sort desc')->select();
        $list = db('jq_base_cate')->where([
            'is_delete' => 0,
            'parent_id' => array(
                'gt',
                0
            )
        ])->select();
        foreach ($parent as &$v) {
            $v['create_time'] = date('Y-m-d', $v['create_time']);

            $v['children'] = [];
            foreach ($list as &$vv) {
                if ($vv['parent_id'] == $v['id']) {
                    $v['children'][] = $vv;
                }
            }
        }
        return json([
            'code' => 1,
            'msg'  => 'success',
            'data' => $parent
        ]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     *  zs_question       zs_question_option
     *  zs_question_type
     */
    public function add()
    {
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['title'] = $this->request->param('title');   // 1单选 2多选3  填空
        $data['sort'] = $this->request->param('sort');   // 1单选 2多选3  填空
        $data['status'] = $this->request->param('status', '1');
//        $data['iswarn'] = $this->request->param('iswarn');
//        $data['page_type']=$this->request->param('page_type');
        $parent_id = $this->request->param('parent_id');
        $data['parent_id'] = $parent_id ? $parent_id : 0;
        $data['level'] = $parent_id ? 1 : 0;
        // 校验 
        $repeat_map = [];
        $repeat_map['parent_id'] = $data['parent_id'];
        $repeat_map['is_delete'] = 0;
        $repeat_map['title'] = $data['title'];
        $repeat = $this->model->where($repeat_map)->find();
        if ($repeat)  {
            $this->error('类目名称已存在');
        }
        $res = db('jq_base_cate')->insert($data);
        if (!$res) {
            return json([
                'code' => 0,
                'msg'  => '操作失败',
                'data' => ''
            ]);
        }
        return json([
            'code' => 1,
            'msg'  => '操作成功',
            'data' => ''
        ]);
    }


    /**
     * 删除
     */
    public function del()
    {
        $id = $this->request->param('id');

        $r = db('jq_base_cate')->where([
            'id'        => $id,
            'is_delete' => 0
        ])->find();
        if (empty($r)) {
            return json([
                'code' => 0,
                'msg'  => '参数有误',
                'data' => ''
            ]);
        }
        $r = db('jq_base_cate')->where([
            'parent_id' => $id,
            'is_delete' => 0
        ])->find();
        if (!empty($r)) {
            return json([
                'code' => 0,
                'msg'  => '此数据包含子项目',
                'data' => ''
            ]);
        }
        $r = db('jq_base_cate')->where(['id' => $id])->update(['is_delete' => 1]);
        if (!$r) {
            return json([
                'code' => 0,
                'msg'  => '操作失败'
            ]);
        }
        return json([
            'code' => 1,
            'msg'  => '操作成功'
        ]);

    }


    public function edit()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            $this->error('参数有误，请核查');
        }
        $data = $this->request->param();
        $detail = $this->model->get($id);

        if (empty($detail)) { 
            $this->error('类目不存在');
        }
        $data['level'] = (isset($data['parent_id']) && $data['parent_id']) ? 1 : 0; 
        
         // 校验  
         if (isset($data['title']) && $data['title']) { 
                $repeat_map = [];
                $repeat_map['parent_id'] = $data['parent_id'];
                $repeat_map['is_delete'] = 0;
                $repeat_map['title'] = $data['title'];
                $repeat_map['id'] = ['neq', $id]; 
                $repeat = $this->model->where($repeat_map)->find();
                if ($repeat)  {
                    $this->error('类目名称已存在');
                }
         }
        
        $result = $detail->save($data);

        if ($result == false) {
            $this->error('编辑失败');
        }
        $this->success('编辑成功');
    }


    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit_discard()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return json([
                'code' => 0,
                'msg'  => '参数有误，请核查'
            ]);
        }

        $data = $this->request->param();
        $data['update_time'] = time();
//        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
//        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
//        $data['status']=$this->request->param('status');
//        $data['iswarn']=$this->request->param('iswarn');
        $parent_id = $this->request->param('parent_id');
//        $data['page_type']=$this->request->param('page_type');
        $data['parent_id'] = $parent_id ? $parent_id : 0;
        $data['level'] = $parent_id ? 1 : 0;
//        var_dump( $data);die;
        $where['id'] = $id;
        $r = db('jq_base_cate')->where($where)->update($data);
        if (!$r) {
            return json([
                'code' => 0,
                'msg'  => '操作失败，请重试'
            ]);
        }
        return json([
            'code' => 1,
            'msg'  => '操作成功'
        ]);
    }


    public function edit_disabled()
    {
        $id = $this->request->param('id');
        $type = $this->request->param('type');
        if (empty($id)) {
            return json([
                'code' => 0,
                'msg'  => '参数有误，请核查'
            ]);
        }

        $rinfo = db('jq_base_cate')->where([
            'id'        => $id,
            'is_delete' => 0
        ])->find();
        if (empty($rinfo)) {
            return json([
                'code' => 0,
                'msg'  => '未查询到信息',
                'data' => ''
            ]);
        }

        $data['status'] = $this->request->param('status');
        $data['update_time'] = time();

        $where['id'] = $id;
        $r = db('jq_base_cate')->where($where)->update($data);
        if (!$r) {
            return json([
                'code' => 0,
                'msg'  => '操作失败，请重试'
            ]);
        }


//        $r = db('jq_base_cate')->where(['parent_id'=>$id,'is_delete'=>0])->find();
//        if(!empty($r)){
//            return json(['code'=>0,'msg'=>'此数据包含子项目','data'=>'']);
//        }

        // 一级操作二级跟着操作
        if ($rinfo['parent_id'] == 0) {
            $listchild = db('jq_base_cate')->where([
                'parent_id' => $id,
                'is_delete' => 0
            ])->select();
            if ($listchild) {
                $where1['parent_id'] = $id;
                $where1['is_delete'] = 0;
                $data1 = array();
                $data1['status'] = $this->request->param('status');
                $data1['update_time'] = time();
                $r = db('jq_base_cate')->where($where1)->update($data1);
            }
        } else {
            // 二级修改更新一级
            (new BaseCategory())->updateParentCate($rinfo);
        }


        return json([
            'code' => 1,
            'msg'  => '操作成功'
        ]);
    }


    // 告警
    public function edit_warn()
    {
        $id = $this->request->param('id');

        $type = $this->request->param('type');


        if (empty($id)) {
            return json([
                'code' => 0,
                'msg'  => '参数有误，请核查'
            ]);
        }

        $rinfo = db('jq_base_cate')->where([
            'id'        => $id,
            'is_delete' => 0
        ])->find();
        if (empty($rinfo)) {
            return json([
                'code' => 0,
                'msg'  => '未查询到信息',
                'data' => ''
            ]);
        }


        $data['iswarn'] = $this->request->param('status');

        $data['update_time'] = time();

        $where['id'] = $id;
        $r = db('jq_base_cate')->where($where)->update($data);
        if (!$r) {
            return json([
                'code' => 0,
                'msg'  => '操作失败，请重试'
            ]);
        }

        //        $r = db('jq_base_cate')->where(['parent_id'=>$id,'is_delete'=>0])->find();
//        if(!empty($r)){
//            return json(['code'=>0,'msg'=>'此数据包含子项目','data'=>'']);
//        }

        if ($rinfo['parent_id'] == 0) {
            $listchild = db('jq_base_cate')->where([
                'parent_id' => $id,
                'is_delete' => 0
            ])->select();
            if ($listchild) {
                $where1['parent_id'] = $id;
                $where1['is_delete'] = 0;
                $data1 = array();
                $data1['iswarn'] = $this->request->param('status');
                $data1['update_time'] = time();
                $r = db('jq_base_cate')->where($where1)->update($data1);
            }
        } else {
            // 二级修改更新一级
            (new BaseCategory())->updateParentCateWarin($rinfo);
        }
        return json([
            'code' => 1,
            'msg'  => '操作成功'
        ]);
    }


}